﻿(function ($) {
    var methods = {
        add: function (options) {
            var appmsgSettings = {
                'id': 0,
                'level': null,
                'message': null,
                'description': null,
                'header': null,
                'seconds': 0,
                'parentElement': null
            };

            return this.each(function () {
                var me = $(this);
                if (options) {
                    $.extend(appmsgSettings, options);
                }

                me.after(' \
                    <div id="' + appmsgSettings.parentElement + 'appmsg' + appmsgSettings.id + '" class="ui-widget ui-corner-all appmsg' + appmsgSettings.level + '" data-opus-appmsg-level="' + appmsgSettings.level + '" data-opus-appmsg-seconds="' + appmsgSettings.seconds + '" > \
                    <a id="' + appmsgSettings.parentElement + 'appmsgExpand' + appmsgSettings.id + '" class="appmsgImgClosed appmsgButton" style="float: left; margin-right: .3em;"></a> \
                    <a id="' + appmsgSettings.parentElement + 'appmsgAccept' + appmsgSettings.id + '" class="appmsgButton" style="float: left; margin-right: .3em;"></a> \
                    <span class="ui-icon ' + (appmsgSettings.level === "Error" ? 'ui-icon-alert' : appmsgSettings.level === "Warning" ? 'ui-icon-notice' : 'ui-icon-info') + '" style="float: left; margin-left: 10px; margin-right: .3em;"></span> \
                    <span class="appmsgHeader">' + appmsgSettings.header + '</span> \
                    <span class="appmsgMessage">' + appmsgSettings.message + '</span> \
                    <span id="' + appmsgSettings.parentElement + 'appmsgSeconds' + appmsgSettings.id + '" style="display: none;"></span> \
                    <p class="appmsgDetails" id="' + appmsgSettings.parentElement + 'appmsgDetails' + appmsgSettings.id + '" style="display: none"><br/>' + appmsgSettings.description + '</p> \
                    </div>');

                var times = appmsgSettings.seconds;
                $('#' + appmsgSettings.parentElement + 'appmsg' + appmsgSettings.id + '[data-opus-appmsg-seconds!="0"]').everyTime(1000, 'timeoutLabel' + appmsgSettings.id, function (i) {
                    $('#' + appmsgSettings.parentElement + 'appmsgSeconds' + appmsgSettings.id).show();
                    appmsgSettings.seconds = appmsgSettings.seconds - 1;
                    $('#' + appmsgSettings.parentElement + 'appmsgSeconds' + appmsgSettings.id + ' > span').remove();
                    $('#' + appmsgSettings.parentElement + 'appmsgSeconds' + appmsgSettings.id).append('<span>.  This message will close in ' + (appmsgSettings.seconds + 1) + ' seconds (<a id="' + appmsgSettings.parentElement + 'appmsgSecondsCancel' + appmsgSettings.id + '" href="#">Cancel</a>).</span>');
                    if (i == times) {
                        $.ajax({
                            type: "POST",
                            url: "/Shared/AcceptApplicationMessage",
                            data: "{'id':'" + appmsgSettings.id + "'}",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (msg) {
                                $('#' + appmsgSettings.parentElement + 'appmsg' + appmsgSettings.id + '[data-opus-appmsg-seconds!="0"]').stopTime('timeoutLabel' + appmsgSettings.id);
                                $('#' + appmsgSettings.parentElement + 'appmsg' + appmsgSettings.id).hide().remove();
                            }
                        });
                    }
                }, times);

                $('#' + appmsgSettings.parentElement + 'appmsgSecondsCancel' + appmsgSettings.id).live('click', function (e) {
                    $('#' + appmsgSettings.parentElement + 'appmsg' + appmsgSettings.id + '[data-opus-appmsg-seconds!="0"]').stopTime('timeoutLabel' + appmsgSettings.id);
                    $('#' + appmsgSettings.parentElement + 'appmsgSeconds' + appmsgSettings.id + ' > span').remove();
                });

                $('#' + appmsgSettings.parentElement + 'appmsgExpand' + appmsgSettings.id).button({ icons: { primary: "ui-icon ui-icon-plus" }, text: false })
                    .click(function () {
                        var id = appmsgSettings.id;

                        if ($(this).hasClass('appmsgImgClosed')) {
                            $(this).removeClass('appmsgImgClosed').addClass('appmsgImgOpen');
                            $(this).button("option", "icons", { primary: 'ui-icon ui-icon-minus' });
                        }
                        else {
                            $(this).removeClass('appmsgImgOpen').addClass('appmsgImgClosed');
                            $(this).button("option", "icons", { primary: 'ui-icon ui-icon-plus' });
                        }

                        $('#' + appmsgSettings.parentElement + 'appmsgDetails' + id).toggle();
                    });

                    $('#' + appmsgSettings.parentElement + 'appmsgAccept' + appmsgSettings.id).button({ icons: { primary: "ui-icon-check" }, text: false })
                        .click(function() {
                            var id = appmsgSettings.id;

                            $.ajax({
                                type: "POST",
                                url: "/Shared/AcceptApplicationMessage",
                                data: "{'id':'" + id + "'}",
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function (msg) {
                                    $('#' + appmsgSettings.parentElement + 'appmsg' + id).hide().remove();
                                }
                            });
                    });
            });
        },
        destroy: function () {
            return this.each(function () {
                $(this).unbind('.applicationMessage');
            });
        }
    };

    $.fn.applicationMessage = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.applicationMessage');
        }
    };
})(jQuery);

